草庐IT

php - 匿名类构造

全部标签

javascript - 这个函数类似于构造函数吗?

functioncatRecord(name,birthdate,mother){return{name:name,birth:birthdate,mother:mother};}我对构造函数的理解是一个用于构建对象的函数。这个catRecord函数算作构造函数吗? 最佳答案 不,您提供的函数返回一个没有类型信息的新关联数组。您所追求的构造函数是这样的:functionfixedCatRecord(name,birthdate,mother){this.name=name;this.birthdate=birthdate;this.

javascript - Firefox ES6,获取类构造函数名称

在Firefox中使用ES6类时,我在获取构造函数的名称时遇到问题。在Chromium中它工作正常,但Firefox似乎有某种错误?在Firefox中,我只返回一个空字符串。有人知道解决方法吗?classMyClass{}leta=newMyClass();console.log(a.constructor.name); 最佳答案 我认为这是一个错误(根据下面的评论)。似乎指定显式构造函数在Firefox中表现出正确的行为(即使是最新版本48)。classMyClassWithConstructor{constructor(){co

javascript - 是否有与 PHP 的 include 等效的 Node.js,以便包含的代码可以访问父文件的变量?

我想将我的Node应用程序拆分成几个单独的文件,以使其更加模块化且更易于维护。但是由于无法像PHP等其他语言那样将文件直接“包含”到当前解析的文件中,因此我的“模块”或“单独文件”不会自动访问脚本中定义的变量“需要”他们。我该怎么做?我正在考虑在我的单独文件中做这样的事情:module.exports=function(stuff){//Inowhaveaccessto'stuff'.}但是有点麻烦。我确定有人已经在我之前解决了这个问题,所以...您有什么建议? 最佳答案 跨模块共享变量的最简单方法是将变量分配给全局命名空间对象。声

javascript - typescript 覆盖构造函数中的扩展属性

我在使用Typescript时遇到问题,我扩展了一个类并从父类(superclass)覆盖了一个属性,但是当我实例化子类时,父类(superclass)属性仍然在构造函数中读取。请看下面的例子:classPerson{publictype:string='GenericPerson';publicconstructor(){console.log(this.type);}}classClownextendsPerson{publictype:string='ScaryClown';}varperson=newPerson(),//'GenericPerson'clown=newClow

javascript - 构造函数调用绑定(bind)函数的代理

假设我有一个函数Foo,我希望从它构造的对象有一个bar属性:functionFoo(){}Foo.prototype.bar='baz'console.log('newFoo().bar:'+newFoo().bar)newFoo().bar:baz现在假设我bindFoo以某种方式。绑定(bind)函数仍然可以在构造函数调用中使用,绑定(bind)的this将被忽略:constBound=Foo.bind(42)console.log('newBound().bar:'+newBound().bar)newBound().bar:bazProxies应该是一般和透明的。然而……co

javascript - 将 object.constructor 与其构造函数和 instanceof 进行比较有什么区别?

这个问题在这里已经有了答案:What'sthedifferencebetweenusinginstanceofandcheckingtheconstructor?(2个答案)Differencebetweeninstanceofandconstructorproperty(2个答案)关闭4年前。假设我有一个Dog构造函数functionDog(name){this.name=name;}我有一个构造函数的实例constmyDog=newDog('Charlie');据我最近了解到,有两种方法可以检查myDog是否是Dog的实例:1.console.log(myDoginstanceof

javascript - 为什么 jQuery 构造函数映射到 jQuery.fn.init?

这个问题在这里已经有了答案:WhyistheinitfunctioninjQuery.prototypeandnotinjQuery'sclosure?(1个回答)JQuerysourcecodequestions(2个答案)关闭9年前。jQuery构造函数将其功能映射到另一个构造函数,jQuery.fn.init:jQuery=function(selector,context){returnnewjQuery.fn.init(selector,context,rootjQuery);},我想知道为什么。Thisquestionisverysimilar,buteventheansw

javascript - 使用 Date 对象调用 Date 构造函数

Date的JS文档声称有四种方法可以使用Date构造函数。来自https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date:newDate();newDate(value);//integernewDate(dateString);//stringnewDate(year,month[,day[,hour[,minutes[,seconds[,milliseconds]]]]]);但是,似乎还有第五种使用构造函数的方法,即传递一个有效的日期对象。例如,以下在chrome控制台中

javascript - fabric.Canvas 不是构造函数

我像这样包含了织物:但是当我这样使用它时:varcanvas;functioninitSketchPad(){canvas=newfabric.Canvas('sketch-pad',{isDrawingMode:true});}我明白了UncaughtTypeError:fabric.Canvasisnotaconstructor 最佳答案 我不确定您的fabric_freedrawing.js文件中有什么,但将您的代码添加到下面的代码片段似乎工作正常。varcanvas;functioninitSketchPad(){canva

javascript - 有没有可能在调用构造函数之前就知道 'this'这个对象呢?

在ES6类之前,函数可以用作构造函数:functionMyClass(a,b){}那么,下面的代码就相当于一个经典的实例化(比如letthisObj=newMyClass("A","B")):letthisObj=Object.create(MyClass.prototype)//Hereweknowthe`this`objectbeforetocalltheconstructor.//Then,theconstructoriscalledmanually:MyClass.call(thisObj,"A","B")...这种技术是一种在调用构造函数之前了解this对象的方法。但是Fun